const app = getApp()
let phoneReg = /^[1][0-9]{10}$/
import {
  throttle
} from "../../../utils/util"
import {
  userFindAll,
  userDelete,
  userInsert,
  download
} from "../../../public/api/person.js"
import { deviceFindAll,userBindDevice } from "../../../public/api/device.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    translations: {},
    language: '',
    uuid: '',
    deviceName: '',
    phone: '',
    personList: [],
    deviceList: [],
    hideModal: true,
    hideModal2: true,
    optType: 'add',
    currentUuid: '',
    userId: '',
    userInfo: {},
    userType: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options);
    this.setData({
      uuid: options.uuid,
      deviceName: options.name,
      currentUuid: options.uuid,
      userType: options.userType
    })
    this.getUserList(options.uuid)
    this.getDeviceList()
  },

  getUserList(uuid) {
    let that = this
    uuid = uuid ? uuid : this.data.uuid
    userFindAll({
      uuid
    }).then(res => {
      if (res.data.code === 1) {
        let userId = wx.getStorageSync('userInfo').id
        let personList = res.data.data.filter(ele => ele.id != userId)
        console.log(personList);
        this.setData({
          personList
        })
        personList.map(item => {
          if(item.photoId) {
            that.getPhoto(item.id, item.photoId)
          } 
        })
       
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },

  // 查询照片
  getPhoto(id,photoId) {
    let personIndex = this.data.personList.findIndex(v => v.id == id)
    let person = this.data.personList[personIndex]
    console.log(person);
    download({id: photoId}).then(res => {
      if(res.statusCode === 200) {
        person.url = res.data
        this.setData({
          ['personList[' + personIndex + ']']: person
        })
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },

  getDeviceList(uuid) {
    uuid = uuid ? uuid : ''
    deviceFindAll({uuid}).then(res => {
      if(res.data.code === 1) {
        this.setData({deviceList: res.data.data})
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },

  doOperation(e) {
    let that = this
    let optType = e.currentTarget.dataset.type
    if (optType == 'delete') {
      let userId = e.currentTarget.dataset.id
      wx.showModal({
        title: '删除人员',
        content: '删除后该号码将不能使用此设备',
        complete: (res) => {
          if (res.confirm) {
            let data = {}
            data.uuid = that.data.uuid
            data.userId = userId
            userDelete(data).then(res => {
              if (res.data.code === 1) {
                wx.showToast({
                  title: "删除成功",
                  icon: 'none'
                })
                that.getUserList()
              } else {
                wx.showToast({
                  title: res.data.msg,
                  icon: 'none'
                })
              }
            })
          }
        }
      })
    } else {
      this.setData({hideModal: false})
    }
    console.log(e);
    this.setData({
      optType
    })
  },

  doHandle(e) {
    let that = this
    let type = e.currentTarget.dataset.type
    console.log(type);
    switch (type) {
      case "cancel":
        this.setData({
          hideModal: true
        })
        break;
      case "add":
        if(that.data.phone === '') {
          wx.showToast({
            title: that.data.translations.person.enter_authorized_by_wechat,
            icon: 'none'
          })
          return false
        }
        if(!phoneReg.test(this.data.phone)) {
          wx.showToast({
            title: '手机号格式不正确',
            icon: 'none'
          })
          return false
        }
        let data = {}
        data.uuids = [that.data.uuid]
        data.phone = that.data.phone
        userBindDevice(data).then(res => {
          if(res.data.code === 1) {
            wx.showToast({
              title: '添加成功',
              icon: 'success'
            })
            that.getUserList()
            that.setData({
              hideModal: true
            })
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
        })
        break;
      default:
        break;
    }

  },

  doChangeDevice() {
    this.setData({
      hideModal2: false
    })
  },

  chooseDevice(e) {
    let currentUuid = e.currentTarget.dataset.uuid
    let deviceName = e.currentTarget.dataset.name
    this.setData({
      currentUuid,
      deviceName
    })
  },

  chooseDeviceConfirm() {
    this.getUserList(this.data.currentUuid)
    this.setData({
      uuid: this.data.currentUuid,
      hideModal2: true
    })
  },

  doCancelDevice() {
    this.setData({
      hideModal2: true
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let language = wx.getStorageSync('language')
    const translations = app.globalData.translations;
    const userInfo = {}
    userInfo.name = app.globalData.userInfo ? app.globalData.userInfo.name : ''
    userInfo.faceData = app.globalData.userInfo ? app.globalData.userInfo.faceData : ''
    this.setData({
      language,
      translations,
      userInfo
    })
    wx.setNavigationBarTitle({
      title: translations.nav.personnel_management
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})